package com.faasadmin.faas.modules.admin.admin.controller.system.oauth2;

import cn.hutool.core.collection.CollUtil;
import com.faasadmin.faas.business.core.module.system.convert.oauth2.SysOAuth2UserConvert;
import com.faasadmin.faas.business.core.module.system.service.dept.SysDeptBussService;
import com.faasadmin.faas.business.core.module.system.service.dept.SysPostBussService;
import com.faasadmin.faas.services.system.dal.dataobject.dept.SysDeptDO;
import com.faasadmin.faas.services.system.dal.dataobject.dept.SysPostDO;
import com.faasadmin.faas.services.system.dal.dataobject.user.SysUserDO;
import com.faasadmin.faas.business.core.module.system.service.user.SysUserBussService;
import com.faasadmin.faas.services.system.vo.oauth2.user.SysOAuth2UserInfoRespVO;
import com.faasadmin.faas.services.system.vo.oauth2.user.SysOAuth2UserUpdateReqVO;
import com.faasadmin.framework.common.pojo.CommonResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static com.faasadmin.framework.common.pojo.CommonResult.success;
import static com.faasadmin.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * 提供给外部应用调用为主
 *
 * 1. 在 getUserInfo 方法上，添加 @PreAuthorize("@ss.hasScope('user.read')") 注解，声明需要满足 scope = user.read
 * 2. 在 updateUserInfo 方法上，添加 @PreAuthorize("@ss.hasScope('user.write')") 注解，声明需要满足 scope = user.write
 *
 * @author faasadmin
 */
@Tag(name = "管理后台 - OAuth2.0 用户")
@RestController
@RequestMapping("/system/oauth2/user")
@Validated
@Slf4j
public class SysOAuth2UserController {

    @Resource
    private SysUserBussService userService;
    @Resource
    private SysDeptBussService deptService;
    @Resource
    private SysPostBussService postService;

    @GetMapping("/get")
    @Operation(summary = "获得用户基本信息")
    @PreAuthorize("@ss.hasScope('user.read')")
    public CommonResult<SysOAuth2UserInfoRespVO> getUserInfo() {
        // 获得用户基本信息
        SysUserDO user = userService.getUser(getLoginUserId());
        SysOAuth2UserInfoRespVO resp = SysOAuth2UserConvert.INSTANCE.convert(user);
        // 获得部门信息
        if (user.getDeptId() != null) {
            SysDeptDO dept = deptService.getDept(user.getDeptId());
            resp.setDept(SysOAuth2UserConvert.INSTANCE.convert(dept));
        }
        // 获得岗位信息
        if (CollUtil.isNotEmpty(user.getPostIds())) {
            List<SysPostDO> posts = postService.getPosts(user.getPostIds());
            resp.setPosts(SysOAuth2UserConvert.INSTANCE.convertList(posts));
        }
        return success(resp);
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户基本信息")
    @PreAuthorize("@ss.hasScope('user.write')")
    public CommonResult<Boolean> updateUserInfo(@Valid @RequestBody SysOAuth2UserUpdateReqVO reqVO) {
        // 这里将 UserProfileUpdateReqVO =》UserProfileUpdateReqVO 对象，实现接口的复用。
        // 主要是，AdminUserService 没有自己的 BO 对象，所以复用只能这么做
        userService.updateUserProfile(getLoginUserId(), SysOAuth2UserConvert.INSTANCE.convert(reqVO));
        return success(true);
    }

}
